package com.lyw.GeoLocationClient.ui.adapter;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lyw.GeoLocationClient.R;
import com.lyw.GeoLocationClient.utils.IconManager;
import com.lyw.GeoLocationClient.utils.StringUtils;
import com.lyw.GeoLocationClient.ui.GeotableItem;
import com.xlistview.IphoneTreeView;

import java.util.ArrayList;
import java.util.HashMap;

import static com.lyw.GeoLocationClient.utils.LogUtils.LOGD;

public class GroupListAdapter extends BaseExpandableListAdapter implements IphoneTreeView.IphoneTreeHeaderAdapter {

    private ArrayList<String> mGroup;// 组名
    private HashMap<String, ArrayList<GeotableItem>> mChildren;// 每一组对应的child
    private Context mContext;
    private IconManager iconManager;
    private IphoneTreeView mIphoneTreeView;


    public GroupListAdapter(Context context, IphoneTreeView iphoneTreeView, ArrayList<String> group, HashMap<String, ArrayList<GeotableItem>> map) {
        mContext = context;
        mGroup = group;
        mChildren = map;
        iconManager = IconManager.getInstance();
        mIphoneTreeView = iphoneTreeView;
    }

    public void requery(ArrayList<String> group, HashMap<String, ArrayList<GeotableItem>> map) {
        mGroup = group;
        mChildren = map;
        notifyDataSetChanged();
    }


    public Object getGroup(int groupPosition) {
        return mGroup.get(groupPosition);
    }

    public String getGroupName(int groupPosition) {
        String group = mGroup.get(groupPosition);
        if (StringUtils.isNullOrEmpty(group)) {
            group = mContext.getResources().getString(R.string.ungroup);
        }
        return group;
    }

    public int getGroupCount() {
        return mGroup.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }



    public Object getChild(int groupPosition, int childPosition) {
        String key = mGroup.get(groupPosition);
//        LOG("getchildren " + key + mChildren.toString());
        return mChildren.get(key).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        if (mGroup == null || mGroup.size() <= 0) {
            return 0;
        }
        String key = mGroup.get(groupPosition);
        if (key == null) key = "";
        if (mChildren.get(key) != null) {
            return mChildren.get(key).size();
        }
        return 0;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.contact_buddy_list_group, null);
        }
        TextView groupName = null;
        if (convertView != null) {
            groupName = (TextView) convertView
                    .findViewById(R.id.group_name);
            groupName.setText(String.valueOf(getGroupName(groupPosition)));
            TextView onlineNum = (TextView) convertView
                    .findViewById(R.id.online_count);
            onlineNum.setText(String.valueOf(getChildrenCount(groupPosition)));
            ImageView indicator = (ImageView) convertView
                    .findViewById(R.id.group_indicator);
            if (isExpanded)
                indicator.setImageResource(R.drawable.indicator_expanded);
            else
                indicator.setImageResource(R.drawable.indicator_unexpanded);
            // 必须使用资源Id当key（不是资源id会出现运行时异常），android本意应该是想用tag来保存资源id对应组件。
            // 将groupPosition，childPosition通过setTag保存,在onItemLongClick方法中就可以通过view参数直接拿到了
            convertView.setTag(R.id.xxx01, groupPosition);
            convertView.setTag(R.id.xxx02, -1);
        }
        groupStatusMap.put(groupPosition, isExpanded ? 1 : 0);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ItemLayout il = convertView == null ? new ItemLayout(mContext)
                : (ItemLayout) convertView;

        final GeotableItem geotableItem = (GeotableItem)getChild(groupPosition, childPosition);
        if (geotableItem != null) {
            il.setTag(geotableItem.getTitle());


            il.mTitle.setText(geotableItem.getTitle());
            il.mTitle.setTextColor(mContext.getResources().getColor(iconManager.getColor(childPosition)));
            il.mImage.setImageResource(iconManager.getIcon(childPosition));
        }
//            updateBackground(position , il);

        il.setTag(R.id.xxx01, groupPosition);
        il.setTag(R.id.xxx02, childPosition);

        return il;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

    @Override
    public int getTreeHeaderState(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !mIphoneTreeView.isGroupExpanded(groupPosition)) {

            if (groupStatusMap.get(groupPosition) != null && groupStatusMap.get(groupPosition) == 0) {

                return PINNED_HEADER_GONE;
            } else {
                return PINNED_HEADER_VISIBLE;
            }
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureTreeHeader(View header, int groupPosition,
                                    int childPosition, int alpha) {
        // TODO Auto-generated method stub
        ((TextView) header.findViewById(R.id.group_name))
                .setText(String.valueOf(getGroupName(groupPosition)));
        ((TextView) header.findViewById(R.id.online_count))
                .setText(String.valueOf(getChildrenCount(groupPosition)));
    }

    private HashMap<Integer, Integer> groupStatusMap = new HashMap<Integer, Integer>();

    @Override
    public void onHeadViewClick(int groupPosition, int status) {
        // TODO Auto-generated method stub
        groupStatusMap.put(groupPosition, status);
        LOG("onHeadViewClick");
    }

    @Override
    public int getHeadViewClickStatus(int groupPosition) {
        if (groupStatusMap.containsKey(groupPosition)) {
            return groupStatusMap.get(groupPosition);
        } else {
            return IphoneTreeView.COLLAPSED;
        }
    }

    @Override
    public int getChildCount(int groupPosition) {
        if (getGroupCount() <= 0) {
            return 0;
        }
        return getChildrenCount(groupPosition);
    }

    private class ItemLayout extends LinearLayout {
        TextView mTitle;

        CheckBox mCheckBox;
        ImageView mImage;

        public ItemLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public ItemLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

        public ItemLayout(Context context) {
            super(context);
            LayoutInflater.from(context).inflate(
                    R.layout.item_row_list, this, true);

            mTitle = (TextView) findViewById(R.id.item_title);
            mCheckBox = (CheckBox) findViewById(R.id.item_checkbox);
            mImage = (ImageView) findViewById(R.id.item_icon);
        }
    }

    public void LOG(String msg) {
        LOGD(GroupListAdapter.class, msg);
    }

}
